package com.handong.handdemo.util;


import com.handong.handdemo.entitiss.FootRingNewEntity;

import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

import cn.pda.serialport.Tools;

/**
 * Created by userKey on 2018/4/13.
 */

public class FootRingNewUtil {

    public static boolean checkListMyBingding(List<FootRingNewEntity> mRingList){
        for (FootRingNewEntity et:mRingList){
            if(et.isMyBingding()){
                return true;
            }
        }
        return false;
    }

    public static boolean checkFootRingExsit(Vector<FootRingNewEntity> mRingList, FootRingNewEntity entity){
        for (FootRingNewEntity et:mRingList){
            if(et.getRingID().equals(entity.getRingID())){
                return true;
            }
        }
        return false;
    }
    public static boolean checkFootRingExsit(CopyOnWriteArrayList<FootRingNewEntity> mRingList, byte[] buff){
        for (FootRingNewEntity et:mRingList){
            if(getFootRingFromList(buff,mRingList)!=null){
                return true;
            }
        }
        return false;
    }

    public static  FootRingNewEntity getFootRingFromList(byte[] buff, CopyOnWriteArrayList<FootRingNewEntity> mRingList){
        String id = Tools.byteLinkByteToHexString(buff, 3, 2);
        for (FootRingNewEntity et:mRingList){
            if(et.getRingID().equals(id)){
                return et;
            }
        }
        return null;
    }

    public static FootRingNewEntity findFootRingById(CopyOnWriteArrayList<FootRingNewEntity> mRingList, String id){
        for (FootRingNewEntity entity:mRingList){
            if(entity.getRingID().equals(id)){
                return entity;
            }
        }
        return null;
    }

    public static void setMyBingding(FootRingNewEntity entity, String imei_crc){
        entity.setBingding(true);
        if(imei_crc.equals(SPUtil.getStringValue(SPUtil.IMEI_CRC))){
            //我激活绑定的
            entity.setMyBingding(true);
        }else if(imei_crc.equals("0000")){
            entity.setBingding(false);
            entity.setMyBingding(false);
        }else{
            entity.setMyBingding(false);
        }
    }

    public static  void unbind(FootRingNewEntity entity){
        entity.setIMEI_CCR("");
        entity.setMyBingding(false);
        entity.setBingding(false);
    }

    public static void setSeverParamter(byte[] buff, FootRingNewEntity entity, int index) {
        StringBuffer stb=new StringBuffer();
        stb.append(buff[index++]).append(".").append(buff[index++]).append(".").append(buff[index++]).append(".").append(buff[index++]);
        entity.setIp(stb.toString());
        entity.setPort(String.valueOf(Tools.byteLinkByteToInt(buff[index++],buff[index])));
    }
    public static void setSim(byte[] buff, FootRingNewEntity entity, int index) {
        StringBuffer stb=new StringBuffer();
        if(1==buff[index]){
            index++;
            for(int i=0;i<9;i++){
                stb.append(buff[index++]);
            }
            entity.setSim("8986"+stb.toString());
        }else if(2==buff[index]){
            index++;
            for(int i=0;i<7;i++){
                stb.append(buff[index++]);
            }
            entity.setSim(entity.getSim()+stb.toString());
        }
    }
    public static void setVersion(byte[] buff, FootRingNewEntity entity, int index) {
           StringBuffer stb=new StringBuffer();
            for(int i=0;i<10;i++){
                stb.append(buff[index++]);
            }
            entity.setVersion("V"+stb.toString());
    }

}
